package com.xhorse.lockplatform.comm.util;

import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;

import javax.net.ssl.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class HttpClientUtil {

	private volatile static HttpClientUtil ClientUtil;

	private OkHttpClient client = new OkHttpClient.Builder().readTimeout(60,
			TimeUnit.SECONDS).build();

	private Builder SSLclient = new OkHttpClient.Builder();

	// 私有化构造器，防止被外部调用
	private HttpClientUtil() {
	};

	public static HttpClientUtil getInstance() {
		if (ClientUtil == null) {
			synchronized (HttpClientUtil.class) {
				HttpClientUtil httpClientUtil = ClientUtil;
				if (httpClientUtil == null) {
					synchronized (HttpClientUtil.class) {
						if (ClientUtil == null) {
							ClientUtil = new HttpClientUtil();
						}
					}
				}
			}
		}

		return ClientUtil;
	}

	// 获取OkHttpClient对象
	public OkHttpClient getClient() {
		return client;
	}

	public OkHttpClient getSSLClient(String filePath) {
		X509TrustManager trustManager;
		SSLSocketFactory sslSocketFactory;
		// inputStream= new FileInputStream(filePath); // 得到证书的输入流
		try {
			trustManager = new X509TrustManager() {

				@Override
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				@Override
				public void checkServerTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {

				}

				@Override
				public void checkClientTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
				}
			};
			SSLContext sslContext = SSLContext.getInstance("TLS");
			sslContext.init(null, new TrustManager[] { trustManager }, null);
			sslSocketFactory = sslContext.getSocketFactory();

		} catch (GeneralSecurityException e) {
			throw new RuntimeException(e);
		}
		SSLclient.sslSocketFactory(sslSocketFactory, trustManager).build();
		return SSLclient.build();
	}

	public HttpsURLConnection getConnection(String requestUrl,
			String requestMethod) {
		try {
			// 创建SSLContext
			SSLContext sslContext = SSLContext.getInstance("TLS");
			TrustManager[] tm = { new X509TrustManager() {

				@Override
				public void checkClientTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
				}

				@Override
				public void checkServerTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {

				}

				@Override
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			} };
			// 初始化
			sslContext.init(null, tm, new java.security.SecureRandom());
			// 获取SSLSocketFactory对象
			SSLSocketFactory ssf = sslContext.getSocketFactory();
			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(requestMethod);
			// 设置当前实例使用的SSLSoctetFactory
			conn.setSSLSocketFactory(ssf);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String verTokenGenerate(String username){
		byte[] byteArray = DecriptUtil.encryptAES(username, "4c6971d6df46e89f");
		String prefix = ByteTransferUtil.bytesToHexString(byteArray);
		String suffix = DecriptUtil.MD5(username+"aMhFJ6");
		return suffix+prefix;
		
	}

	public static void main(String[] args) {
		File file = new File("C:\\chip_8.json");
		String key = "4c6971d6df46e89f"; //AES解密钥匙
		Long filelength = file.length();
		byte[] filecontent = new byte[filelength.intValue()];  //文件原始字节数组
		try {
			FileInputStream fis = new FileInputStream(file);
			fis.read(filecontent);
			String result = DecriptUtil.decryptAES(filecontent, key);  //解密，返回最终文件json数据
			System.out.println("##########result:"+result);
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}
}
